package runesim.node.connectors;
import java.io.IOException;
import java.nio.BufferUnderflowException;
import org.xsocket.MaxReadSizeExceededException;
import org.xsocket.connection.*;
public class LSConnectionHandle implements IDataHandler, IDisconnectHandler, IConnectHandler {
	
	@Override
	public boolean onData(INonBlockingConnection nbc) throws IOException, BufferUnderflowException, MaxReadSizeExceededException {
		LSConnection.setConnected(true); // Received data... we're
		// still connected
		
		byte opcode = nbc.readByte();
		
		switch (opcode) {
			case IN_WELCOME: // LS handshake OK
				System.out.println("Authorized by login server :)!");
				break;
			
			case IN_NOAUTH: // LS handshake BAD
				System.out.println("Unauthorized login server connection...");
				LSConnection.forceDisconnect();
				break;
		}
		
		return false;
	}
	
	@Override
	public boolean onDisconnect(INonBlockingConnection nbc) throws IOException {
		LSConnection.setConnected(false);
		return false;
	}
	
	@Override
	public boolean onConnect(INonBlockingConnection nbc) throws IOException, BufferUnderflowException, MaxReadSizeExceededException {
		LSConnection.setConnected(true);
		return false;
	}
	
	// Out-written
	public static final byte OUT_HELLO = 16;
	public static final byte OUT_IS_LOGIN_VALID = 50;
	
	// In-read
	public static final byte IN_NON_EXISTING = 8;
	public static final byte IN_WELCOME = 26;
	public static final byte IN_NOAUTH = 66;
}